<?php

namespace app\admin\controller;

class CustomerFollowBase
{
    public function index()
    {
        $query = \app\model\CustomerFollow::field(['customer_follow.id', 'customer_id', 'started_at', 'ended_at', 'electricity_consumption', 'customer_follow.created_at'])->with(['customer'])->order('id', 'desc');

        if (request()->has('createdAt')) {
            $query->whereBetween('created_at', input('createdAt'));
        }

        if (request()->has('salesId')) {
            $query->hasWhere('customer', ['sales_id' => input('salesId')]);
        }

        $models = $query->select();

        return json(['code' => 20000, 'data' => ['customerFollows' => $models]]);
    }

    public function delete()
    {
        $model = \app\model\CustomerFollow::destroy(input('id'));

        return json(['code' => 20000, 'msg' => '移除成功', 'data' => $model]);
    }

    public function update()
    {
        $model = \app\model\CustomerFollow::update(input());

        return json(['code' => 20000, 'msg' => '更新成功', 'data' => $model]);
    }

    public function create()
    {
        $model = \app\model\CustomerFollow::create(input());

        return json(['code' => 20000, 'msg' => '新增成功', 'data' => $model]);
    }

    public function detail()
    {
        $query = \app\model\CustomerFollow::field(['id', 'customer_id', 'started_at', 'ended_at', 'electricity_consumption']);

        if ($id = input('id')) {
            $query->where('id', $id);
        }

        $model = $query->find();

        return json(['code' => 20000, 'data' => ['customerFollow' => $model]]);
    }
}
